/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;

import java.util.*;
/**
 *
 * @author Do Viet Trung
 */

public class Domain {
    public NameServer nameServer;
    public Vector<Host> Hosts;
    public Vector<Domain> subDomains;
    public String domainName,
                    Folder,
                    pathDomain,
                    fileDatabase,
                    fileNS;
    
    Domain() {
        nameServer = new NameServer();
        Hosts = new Vector<Host>();
        subDomains = new Vector<Domain>();
        domainName = "";
        pathDomain = "";
        fileDatabase = "";
        fileNS = "";
        Folder = "";
    }

    Domain (NameServer ns,
            Vector<Host> host,
            String domain_name,
            String path_domain,
            String file_database,
            String file_ns,
            String folder) {
        
        pathDomain = path_domain;
        fileDatabase = file_database;
        fileNS = file_ns;
        Folder = folder;
        domainName = domain_name;
        //Name server
        nameServer = new NameServer(
                ns.getTypeServer(),
                ns.getHostName(),
                ns.getRoot(),
                ns.getSerial(),
                ns.getRefeshTime(),
                ns.getReplyTime(),
                ns.getExpireTime(),
                ns.getMinimumTime(),
                ns.getTTL());

        Hosts = new Vector<Host>();
        subDomains = new Vector<Domain>();
        //create Host
        for(int i = 0; i < host.size(); i++) {
            Host h = new Host(host.elementAt(i));
            Hosts.add(h);
        }

        subDomains = new Vector<Domain>();
    }

    public void loadSubDomains() {
        Vector <String> listFolder =
                FileAndFolder.listFolder(pathDomain + "\\" + domainName);

        findDomain(this, listFolder);
    }
    
    private void findDomain(Domain d , Vector<String> listDomain) {
       for(int i = 0; i < listDomain.size(); i++) {
            String file_Database = "";
            String file_NS = "";
            String domain_Name = "";
            String folder;

            Vector<Host> arrHost;
            NameServer nameServer;
            String str = d.pathDomain + "\\" + d.Folder + "\\" + listDomain.elementAt(i);
            Vector<String> listFile = FileAndFolder.listFile(str);
            
            if(listFile.size() <= 2) {
                for(int j = 0; j < listFile.size(); j++) {
                    if(listFile.elementAt(j).startsWith(listDomain.elementAt(i))) {
                        file_Database = listFile.elementAt(j);
                    }

                    int k = listFile.elementAt(j).indexOf('[');
                    
                    if(k >= 0) {
                        file_NS = listFile.elementAt(j);
                    }
                }
            } else {
                continue;
            }

            try {
                //load name server
                String file = d.pathDomain + "\\" +
                                d.Folder  + "\\" +
                                listDomain.elementAt(i) + "\\" +  file_NS;
                
                nameServer = new NameServer (NameServer.TYPE_SERVER.PRIMARY, file);
                //load host

                Host h = new Host();
                arrHost =  h.buildHost(d.pathDomain + "\\" + d.Folder  + "\\"
                        + listDomain.elementAt(i) + "\\"  + file_Database);
                domain_Name = listDomain.elementAt(i) + "." + d.domainName;
                folder = listDomain.elementAt(i);
                
                if(!domain_Name.endsWith(".")) {
                    domain_Name += ".";
                }

                Domain sub = new Domain (
                        nameServer ,
                        arrHost,
                        domain_Name,
                        d.pathDomain + "\\" + d.Folder,
                        file_Database,
                        file_NS, folder);

                d.subDomains.add(sub);
                //recursive
                int idx = sub.domainName.indexOf(".");
                
                Vector<String> subList =
                        FileAndFolder.listFolder(sub.pathDomain + "\\" + sub.Folder);
                
                if(subList.size()  > 0) {
                    findDomain(sub, subList);
                }
            }catch (Exception s) {
                System.err.println (s);
                return;
            }
        }
    }
}
